﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace MainWebService
{
    public class ScanDetails
    {
        private int clientId;
        private int userId;
        private string hashCode;

        public int ClientId { get { return clientId; } set { clientId = value; } }
        public int UserId { get { return userId; } set { userId = value; } }
        public string HashCode { get { return hashCode; } set { hashCode = value; } }
        public int ServiceId { get; set; }
        public ScanDetails(int clientId, int userId, string hashCode, int serviceId)
        {
            this.clientId = clientId;
            this.userId = userId;
            this.hashCode = hashCode;
            this.ServiceId = serviceId;
        }

        internal List<Scans> GetScans()
        {
            List<Scans> lstScans = new List<Scans>();
            DAO d = new DAO();
            DataSet ds = new DataSet();
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@ClientId", ClientId));
            param.Add(d.AddParameter("@UserId", UserId));
            param.Add(d.AddParameter("@HashCode", HashCode));
            param.Add(d.AddParameter("@ServiceId", ServiceId));
            ds = d.ExecuteDataSet("P_GetScans", param);
            if (Utility.IsValidDataSet(ds))
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    lstScans.Add(new Scans()
                    {
                        ServiceId = Utility.GetInt(ds.Tables[0].Rows[i]["ServiceId"]),
                        StudentIdNumber = Utility.GetInt(ds.Tables[0].Rows[i]["StudentIdNumber"]),
                        ValidityCode = Utility.GetInt(ds.Tables[0].Rows[i]["Validity"]),
                        Grade = Utility.GetInt(ds.Tables[0].Rows[i]["Grade"]),
                        QuestionId = Utility.GetInt(ds.Tables[0].Rows[i]["QuestionId"]),
                        FirstName = Utility.GetString(ds.Tables[0].Rows[i]["FirstName"]),
                        LastName = Utility.GetString(ds.Tables[0].Rows[i]["LastName"]),
                        Note1 = Utility.GetString(ds.Tables[0].Rows[i]["Note1"]),
                        Note2 = Utility.GetString(ds.Tables[0].Rows[i]["Note2"]),
                        Note3 = Utility.GetString(ds.Tables[0].Rows[i]["Note3"]),
                        Note4 = Utility.GetString(ds.Tables[0].Rows[i]["Note4"]),
                        DeviceId = Utility.GetString(ds.Tables[0].Rows[i]["DeviceId"]),
                        DeviceName = Utility.GetString(ds.Tables[0].Rows[i]["DeviceName"]),
                        QuestionResponse = Utility.GetString(ds.Tables[0].Rows[i]["QuestionResponse"]),
                        ResponseValue = Utility.GetString(ds.Tables[0].Rows[i]["Message"]),
                        ScanStatusMessage = Utility.GetString(ds.Tables[0].Rows[i]["Message"])

                    });
                }
            }
            return lstScans;
        }

        internal static string AddScanData(int CreatedBy, int dbId, Scans item)
        {
            object msg = null;
            DAO d = new DAO();
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@DBId", dbId));
            param.Add(d.AddParameter("@StudentIdNumber", item.StudentIdNumber));
            param.Add(d.AddParameter("@FirstName", item.FirstName));
            param.Add(d.AddParameter("@LastName", item.LastName));
            param.Add(d.AddParameter("@Validity", (int)item.ValidityCode));
            param.Add(d.AddParameter("@Grade", item.Grade));
            param.Add(d.AddParameter("@Note1", item.Note1));
            param.Add(d.AddParameter("@Note2", item.Note2));
            param.Add(d.AddParameter("@Note3", item.Note3));
            param.Add(d.AddParameter("@Note4", item.Note4));
            param.Add(d.AddParameter("@DeviceId", item.DeviceId));
            param.Add(d.AddParameter("@DeviceName", item.DeviceName));
            param.Add(d.AddParameter("@CreatedBy", CreatedBy));
            param.Add(d.AddParameter("@Messages", msg, SqlDbType.VarChar, 500, ParameterDirection.InputOutput));
            d.ExecuteQuery("P_AddScanData", param, ref msg);
            return msg.ToString();
        }


        internal static Scans AddScanData(int clientId, int userId, string hashCode, Scans item, ref int scanId)
        {
            scanId = 0;
            DataSet ds = new DataSet();
            DAO d = new DAO();
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@StudentIdNumber", item.StudentIdNumber));

            if (item != null && !string.IsNullOrEmpty(item.FirstName))
                param.Add(d.AddParameter("@FirstName", item.FirstName));
            if (item != null && !string.IsNullOrEmpty(item.LastName))
                param.Add(d.AddParameter("@LastName", item.LastName));
            if (item != null && !string.IsNullOrEmpty(item.ValidityCode.ToString()))
                param.Add(d.AddParameter("@Validity", (int)item.ValidityCode));
            if (item != null && !string.IsNullOrEmpty(item.Grade.ToString()))
                param.Add(d.AddParameter("@Grade", item.Grade));
            if (item != null && !string.IsNullOrEmpty(item.Note1))
                param.Add(d.AddParameter("@Note1", item.Note1));
            if (item != null && !string.IsNullOrEmpty(item.Note2))
                param.Add(d.AddParameter("@Note2", item.Note2));
            if (item != null && !string.IsNullOrEmpty(item.Note3))
                param.Add(d.AddParameter("@Note3", item.Note3));
            if (item != null && !string.IsNullOrEmpty(item.Note4))
                param.Add(d.AddParameter("@Note4", item.Note4));

            param.Add(d.AddParameter("@DeviceId", item.DeviceId));
            param.Add(d.AddParameter("@DeviceName", item.DeviceName));
            param.Add(d.AddParameter("@CreatedBy", userId));
            if (item.ServiceId > 0)
            {
                param.Add(d.AddParameter("@ServiceId", item.ServiceId));
                ds = d.ExecuteDataSet("P_AddScanDataTransactions", param);
                if (Utility.IsValidDataSet(ds))
                {
                    scanId = Utility.GetInt(ds.Tables[0].Rows[0]["ScanId"]);
                    if (scanId > 0 && item.QuestionId > 0)
                    {
                        try
                        {
                            d = new DAO();
                            string _query = "INSERT INTO Questions4Scan VALUES(" + scanId.ToString() + "," + item.QuestionId.ToString() + ",'" + item.QuestionResponse + "','" + DateTime.Now.ToString() + "'," + userId.ToString() + ")";
                            d.ExecuteQuery(_query);
                        }
                        catch
                        {

                        }
                    }
                }
                return FillScans(ds, item);
            }
            else
            {
                scanId = 0;
                return null;
            }
        }

        private static Scans FillScans(DataSet ds, Scans item)
        {
            Scans _scan = null;
            if (Utility.IsValidDataSet(ds))
            {
                _scan = item;
                _scan.ValidityCode = Utility.GetInt(ds.Tables[0].Rows[0]["Validity"]);
                _scan.ScanStatusMessage = Utility.GetString(ds.Tables[0].Rows[0]["Message"]);
                _scan.ResponseValue = Utility.GetString(ds.Tables[0].Rows[0]["Message"]);
            }
            return _scan;
        }

        internal static Scans ValidateScan(int userId, int clientId, string hashCode, int ServiceId, string StudentIdNumber, string DeviceId, string DeviceName)
        {
            try
            {
                int scanId = 0;
                AddScanData(clientId, userId, hashCode, new Scans() { StudentIdNumber = Convert.ToInt32(StudentIdNumber), DeviceId = DeviceId, DeviceName = DeviceId, ServiceId = ServiceId }, ref scanId);

                DAO d = new DAO();
                List<SqlParameter> param = new List<SqlParameter>();
                param.Add(d.AddParameter("@StudentIdNumber", StudentIdNumber));
                param.Add(d.AddParameter("@DeviceId", DeviceId));
                param.Add(d.AddParameter("@DeviceName", DeviceName));
                param.Add(d.AddParameter("@UserId", userId));
                param.Add(d.AddParameter("@ClientId", clientId));
                param.Add(d.AddParameter("@ServiceId", ServiceId));
                DataSet ds = d.ExecuteDataSet("ValidateScan", param);
                if (Utility.IsValidDataSet(ds))
                {
                    return GetScanDetail(ds);
                }
                else
                {
                    return new Scans() { ScanStatusMessage = "Validation Failed" };
                }
            }
            catch
            {
                return new Scans() { ScanStatusMessage = "Validation Failed" };
            }
        }

        private static Scans GetScanDetail(DataSet ds)
        {
            return new Scans()
            {
                StudentIdNumber = Utility.GetInt(ds.Tables[0].Rows[0]["StudentIdNumber"]),
                FirstName = Utility.GetString(ds.Tables[0].Rows[0]["FirstName"]),
                LastName = Utility.GetString(ds.Tables[0].Rows[0]["LastName"]),
                Note1 = Utility.GetString(ds.Tables[0].Rows[0]["Note1"]),
                Note2 = Utility.GetString(ds.Tables[0].Rows[0]["Note2"]),
                Note3 = Utility.GetString(ds.Tables[0].Rows[0]["Note3"]),
                Note4 = Utility.GetString(ds.Tables[0].Rows[0]["Note4"]),
                Grade = Utility.GetInt(ds.Tables[0].Rows[0]["Grade"]),
                ValidityCode = Utility.GetInt(ds.Tables[0].Rows[0]["Validity"]),
                ScanStatusMessage = "Validated"
            };
        }

        internal static List<Scans> GetDataforOfflineValidation(int userId, int clientId, string hashCode, int ServiceId, string DeviceId, string DeviceName)
        {
            List<Scans> lstScan = null;
            DAO d = new DAO();
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@DeviceId", DeviceId));
            param.Add(d.AddParameter("@DeviceName", DeviceName));
            param.Add(d.AddParameter("@UserId", userId));
            param.Add(d.AddParameter("@ClientId", clientId));
            param.Add(d.AddParameter("@ServiceId", ServiceId));
            DataSet ds = d.ExecuteDataSet("GetDataForOfflineValidation", param);
            if (Utility.IsValidDataSet(ds))
            {
                lstScan = new List<Scans>();
                lstScan = FillScansObject(ds);
                return lstScan;
            }
            return new List<Scans>();
        }

        private static List<Scans> FillScansObject(DataSet ds)
        {
            List<Scans> lstScan = new List<Scans>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                lstScan.Add(new Scans()
                {
                    StudentIdNumber = Utility.GetInt(ds.Tables[0].Rows[i]["StudentIdNumber"]),
                    FirstName = Utility.GetString(ds.Tables[0].Rows[i]["FirstName"]),
                    LastName = Utility.GetString(ds.Tables[0].Rows[i]["LastName"]),
                    Note1 = Utility.GetString(ds.Tables[0].Rows[i]["Note1"]),
                    Note2 = Utility.GetString(ds.Tables[0].Rows[i]["Note2"]),
                    Note3 = Utility.GetString(ds.Tables[0].Rows[i]["Note3"]),
                    Note4 = Utility.GetString(ds.Tables[0].Rows[i]["Note4"]),
                    Grade = Utility.GetInt(ds.Tables[0].Rows[i]["Grade"]),
                    ValidityCode = Utility.GetInt(ds.Tables[0].Rows[i]["Validity"]),
                    ServiceId = Utility.GetInt(ds.Tables[0].Rows[i]["ServiceId"]),
                    ScanId = Utility.GetInt(ds.Tables[0].Rows[i]["ScanId"]),
                    UserId = Utility.GetInt(ds.Tables[0].Rows[i]["CreatedBy"])
                });
            }
            return lstScan;
        }

        internal static Scans DatabaseBuilder(int clientId, int userId, int serviceId, int StudentIdNumber, string hashCode, DeviceInfo device)
        {
            int scanId = 0;
            AddScanData(clientId, userId, hashCode, new Scans() { StudentIdNumber = Convert.ToInt32(StudentIdNumber), DeviceId = device.DeviceId, DeviceName = device.DeviceName, ServiceId = serviceId }, ref scanId);

            List<Scans> lstScan = null;
            DAO d = new DAO();
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@UserId", userId));
            param.Add(d.AddParameter("@ClientId", clientId));
            param.Add(d.AddParameter("@HashCode", hashCode));
            param.Add(d.AddParameter("@ServiceId", serviceId));
            param.Add(d.AddParameter("@StudentIdNumber", StudentIdNumber));
            DataSet ds = d.ExecuteDataSet("P_DatabaseBuilder", param);
            if (Utility.IsValidDataSet(ds))
            {
                lstScan = new List<Scans>();
                lstScan = FillScansObject(ds);
                lstScan[0].ResponseValue = "Scan found";
                return lstScan[0];
            }
            return new Scans() { ResponseValue = "Scan not found" };
        }

        internal static bool DatabaseBuilderSaveScans(int clientId, int userId, string hashCode, Scans item)
        {
            DAO d = new DAO();
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@StudentIdNumber", item.StudentIdNumber));
            param.Add(d.AddParameter("@ServiceId", item.ServiceId));
            if (item != null && !string.IsNullOrEmpty(item.FirstName))
                param.Add(d.AddParameter("@FirstName", item.FirstName));
            if (item != null && !string.IsNullOrEmpty(item.LastName))
                param.Add(d.AddParameter("@LastName", item.LastName));
            if (item != null && !string.IsNullOrEmpty(item.ValidityCode.ToString()))
                param.Add(d.AddParameter("@Validity", (int)item.ValidityCode));
            if (item != null && !string.IsNullOrEmpty(item.Grade.ToString()))
                param.Add(d.AddParameter("@Grade", item.Grade));
            if (item != null && !string.IsNullOrEmpty(item.Note1))
                param.Add(d.AddParameter("@Note1", item.Note1));
            if (item != null && !string.IsNullOrEmpty(item.Note2))
                param.Add(d.AddParameter("@Note2", item.Note2));
            if (item != null && !string.IsNullOrEmpty(item.Note3))
                param.Add(d.AddParameter("@Note3", item.Note3));
            if (item != null && !string.IsNullOrEmpty(item.Note4))
                param.Add(d.AddParameter("@Note4", item.Note4));

            param.Add(d.AddParameter("@DeviceId", item.DeviceId));
            param.Add(d.AddParameter("@DeviceName", item.DeviceName));
            param.Add(d.AddParameter("@CreatedBy", userId));

            int result = d.ExecuteQuery("P_DBBuilderAddData", param);

            return result > 0;
        }

        internal static List<Scans> GetScansData(SearchParams item)
        {
            List<Scans> _scans = null;

            DAO d = new DAO();
            List<SqlParameter> param = new List<SqlParameter>();

            if (item != null && item.StudentIdNumber > 0)
                param.Add(d.AddParameter("@StudentIdNumber", item.StudentIdNumber));

            if (item != null && !string.IsNullOrEmpty(item.ServiceIdsList))
                param.Add(d.AddParameter("@ServiceIds", item.ServiceIdsList));

            if (item != null && !string.IsNullOrEmpty(item.UsersList))
                param.Add(d.AddParameter("@UserIds", item.UsersList));

            if (item != null && !string.IsNullOrEmpty(item.StudentName))
                param.Add(d.AddParameter("@StudentName", item.StudentName));

            if (item != null && !string.IsNullOrEmpty(item.DeviceIdsList))
                param.Add(d.AddParameter("@Devices", item.DeviceIdsList));

            if (item != null && !string.IsNullOrEmpty(item.FromDate))
                param.Add(d.AddParameter("@FromDate", item.FromDate));

            if (item != null && !string.IsNullOrEmpty(item.FromTime))
                param.Add(d.AddParameter("@FromTime", item.FromTime));

            if (item != null && !string.IsNullOrEmpty(item.ToDate))
                param.Add(d.AddParameter("@ToDate", item.ToDate));

            if (item != null && !string.IsNullOrEmpty(item.ToTime))
                param.Add(d.AddParameter("@ToTime", item.ToTime));


            param.Add(d.AddParameter("@ClientId", item.ClienId));

            param.Add(d.AddParameter("@HashCode", item.HashCode));

            DataSet result = null;
            try
            {
                result = d.ExecuteDataSet("P_GetScansData", param);
                if (Utility.IsValidDataSet(result))
                {
                    _scans = new List<Scans>();
                    for (int i = 0; i < result.Tables[0].Rows.Count; i++)
                    {
                        _scans.Add(new Scans()
                        {
                            ScanId = Utility.GetInt(result.Tables[0].Rows[i]["ScanId"]),
                            DeviceId = Utility.GetString(result.Tables[0].Rows[i]["DeviceId"]),
                            DeviceName = Utility.GetString(result.Tables[0].Rows[i]["DeviceName"]),
                            FirstName = Utility.GetString(result.Tables[0].Rows[i]["FirstName"]),
                            Grade = Utility.GetInt(result.Tables[0].Rows[i]["Grade"]),
                            LastName = Utility.GetString(result.Tables[0].Rows[i]["LastName"]),
                            Note1 = Utility.GetString(result.Tables[0].Rows[i]["Note1"]),
                            Note2 = Utility.GetString(result.Tables[0].Rows[i]["Note2"]),
                            Note3 = Utility.GetString(result.Tables[0].Rows[i]["Note3"]),
                            Note4 = Utility.GetString(result.Tables[0].Rows[i]["Note4"]),
                            QuestionId = Utility.GetInt(result.Tables[0].Rows[i]["QuestionId"]),
                            QuestionText = Utility.GetString(result.Tables[0].Rows[i]["QuestionText"]),
                            QuestionResponse = Utility.GetString(result.Tables[0].Rows[i]["QuestionResponse"]),
                            ResponseValue = Utility.GetString(result.Tables[0].Rows[i]["Message"]),
                            ScanStatusMessage = Utility.GetString(result.Tables[0].Rows[i]["Message"]),
                            ServiceId = Utility.GetInt(result.Tables[0].Rows[i]["ServiceId"]),
                            StudentIdNumber = Utility.GetInt(result.Tables[0].Rows[i]["StudentIdNumber"]),
                            CreateDate = Utility.GetString(result.Tables[0].Rows[i]["CreatedDate"]),
                            ServiceName = Utility.GetString(result.Tables[0].Rows[i]["ServiceName"]),
                            ServiceSubType = Utility.GetString(result.Tables[0].Rows[i]["ServiceSubType"]),
                            UserId = Utility.GetInt(result.Tables[0].Rows[i]["CreatedBy"]),
                            UserName = Utility.GetString(result.Tables[0].Rows[i]["UserName"]),
                            TimeZone = Utility.GetString(result.Tables[0].Rows[i]["TimeZone"]),
                            ValidityCode = Utility.GetInt(result.Tables[0].Rows[i]["Validity"])
                        }
                                     );
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return _scans;
        }

        internal static List<Scans> SearchScan(int clientId, int serviceId, string hashCode, string searchString)
        {
            List<Scans> _scans = null;
            DAO d = new DAO();
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@ClientId", clientId));
            param.Add(d.AddParameter("@ServiceId", serviceId));
            param.Add(d.AddParameter("@HashCode", hashCode));
            param.Add(d.AddParameter("@SearchString", searchString));
            DataSet result = null;
            try
            {
                result = d.ExecuteDataSet("P_SearchScansData", param);
                if (Utility.IsValidDataSet(result))
                {
                    _scans = new List<Scans>();
                    for (int i = 0; i < result.Tables[0].Rows.Count; i++)
                    {
                        _scans.Add(new Scans()
                        {
                            ScanId = Utility.GetInt(result.Tables[0].Rows[i]["ScanId"]),
                            DeviceId = Utility.GetString(result.Tables[0].Rows[i]["DeviceId"]),
                            DeviceName = Utility.GetString(result.Tables[0].Rows[i]["DeviceName"]),
                            FirstName = Utility.GetString(result.Tables[0].Rows[i]["FirstName"]),
                            Grade = Utility.GetInt(result.Tables[0].Rows[i]["Grade"]),
                            LastName = Utility.GetString(result.Tables[0].Rows[i]["LastName"]),
                            Note1 = Utility.GetString(result.Tables[0].Rows[i]["Note1"]),
                            Note2 = Utility.GetString(result.Tables[0].Rows[i]["Note2"]),
                            Note3 = Utility.GetString(result.Tables[0].Rows[i]["Note3"]),
                            Note4 = Utility.GetString(result.Tables[0].Rows[i]["Note4"]),
                            QuestionId = Utility.GetInt(result.Tables[0].Rows[i]["QuestionId"]),
                            QuestionText = Utility.GetString(result.Tables[0].Rows[i]["QuestionText"]),
                            QuestionResponse = Utility.GetString(result.Tables[0].Rows[i]["QuestionResponse"]),
                            // ResponseValue = Utility.GetString(result.Tables[0].Rows[i]["Message"]),
                            //  ScanStatusMessage = Utility.GetString(result.Tables[0].Rows[i]["Message"]),
                            // ServiceId = Utility.GetInt(result.Tables[0].Rows[i]["ServiceId"]),
                            StudentIdNumber = Utility.GetInt(result.Tables[0].Rows[i]["StudentIdNumber"]),
                            CreateDate = Utility.GetString(result.Tables[0].Rows[i]["CreatedDate"]),
                            //  ServiceName = Utility.GetString(result.Tables[0].Rows[i]["ServiceName"]),
                            //  ServiceSubType = Utility.GetString(result.Tables[0].Rows[i]["ServiceSubType"]),
                            UserId = Utility.GetInt(result.Tables[0].Rows[i]["CreatedBy"]),
                            UserName = Utility.GetString(result.Tables[0].Rows[i]["UserName"]),
                            ValidityCode = Utility.GetInt(result.Tables[0].Rows[i]["Validity"])
                        }
                                     );
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return _scans;
        }

        internal static string ArchiveScanData(int clientId,int scanId,string hashCode)
        {
            string msg="";
            int result = 0;
            DAO d = new DAO();
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(d.AddParameter("@ClientId", clientId));
            param.Add(d.AddParameter("@ScanId", scanId));
            param.Add(d.AddParameter("@HashCode", hashCode));
            result = d.ExecuteQuery("ArchiveScanData", param);
            if (result > 0)
                msg = result.ToString() + " Scans has been archived";
            else
                msg = "No scans to archive";
            return msg;
        }
    }
}